/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
Ext.apply(Ext.form.VTypes, {
    password : function(val, field) {
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },

    passwordText : msg_pass
});

Ext.onReady(function(){
    
	
		Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    	Ext.form.Field.prototype.msgTarget = 'side';
		
		var ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: 'get'
        }),
        reader: new Ext.data.JsonReader({
            root: 'p',
            totalProperty: 'totalCount',
            id: 'id'
        }, [
        		{name: 'first_name'},
    				 {name: 'last_name'},
					 {name: 'institution'},
					 {name: 'email'}          
        ])
    });
		
		
  var editForm = new Ext.FormPanel({
        labelWidth: 100,
		title: lbl_panel_title,
        border:true,
		frame:true,
		bodyStyle:'padding:5px 5px 0',
        labelAlign: 'right',
        width: 350,
        waitMsgTarget: true,
		store: ds,

        items: {
            xtype:'tabpanel',
            activeTab: 0,
            defaults:{autoHeight:true, bodyStyle:'padding:10px'}, 
            items:[{
                title:lbl_form_title,
                layout:'form',
                defaults: {width: 150},
                defaultType: 'textfield',

                items: [{
					fieldLabel: lbl_first_name,
					name: 'first_name',
					allowBlank:false
				},{
					fieldLabel: lbl_last_name,
					name: 'last_name',
					allowBlank:false
				},{
					fieldLabel: lbl_institution,
					name: 'institution',
					allowBlank:false
				}, {
					fieldLabel: lbl_email,
					name: 'email',
					vtype:'email',
					allowBlank:false
				}]
            },{
                title:lbl_change_pass,
                layout:'form',
                defaults: {width: 150},
				labelWidth: 120,
                defaultType: 'textfield',

                items: [ {
					fieldLabel: lbl_cur_pass,
					inputType: 'password',
					name: 'old_pass',
					id: 'old_pass'
				},{
					fieldLabel: lbl_new_pass,
					inputType: 'password',
					name: 'password',
					id: 'pass'
				},{
					fieldLabel: lbl_cfm_pass,                
					inputType: 'password',
					name: 'pass-cfrm',
					vtype: 'password',
					initialPassField: 'pass' 
				}]
            }]
        },

        buttons: [{
            text: btn_label,
			type: 'submit',
            name: 'changes',
			handler: function(){
            	var form = editForm.getForm();
            	if(form.isValid()){
            		form.submit({url:'edit', waitMsg:msg_editing, success: regComplete});
            	}
            }
        }],
		
		renderTo: 'profile-form'
    });
  
  	var regComplete = function(){
		Ext.Msg.alert(lbl_status, msg_success);	
	}
	var loaded = function(r, options, success){
		editForm.getForm().loadRecord(r[0]);
	};
    ds.load({callback: loaded});
	
});